package cn.edu.niit.servlet;

import cn.edu.niit.service.BookService;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/book/delFavorite")
public class DelFavoriteBookServlet extends HttpServlet {
    private BookService bookService=new BookService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, Object> params = new HashMap<String, Object>();
        Map<String, String[]> parameterMap = req.getParameterMap();
        if (parameterMap != null) {
            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                params.put(entry.getKey(), entry.getValue()[0]);
            }
        }
        int id = Integer.parseInt((String) params.get("id"));
        int i = bookService.delFavorite(id);
        Map<String, Object> resultMap = new HashMap<>();
        if (i > 0) {
            resultMap.put("results", "data");
            resultMap.put("errorNo", "0");
            resp.getWriter().print(JSONObject.toJSONString(resultMap));
        }else {
            resultMap.put("results", "");
            resultMap.put("errorInfo", "收藏删除失败");
            resultMap.put("errorNo", "1");
            resp.getWriter().print(JSONObject.toJSONString(resultMap));
        }
    }
}
